package com.itheima.web.servlet.system;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Role;
import com.itheima.utils.BeanUtil;
import com.itheima.web.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: gjp
 * @Description:
 * @Date: Created in 20:35 2020/11/29
 * @ModifiedBy:
 */
@WebServlet("/system/role")
public class RoleServlet extends BaseServlet {


    /**
     * @param request
     * @param response
     * @Description: 分页 角色名称 联合查询 所有 角色(Role)
     * @return:
     * @Author: gjp
     * @Date: 20:35 2020/11/29
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer pageNum = 1;
        Integer pageSize = 5;
        String roleName = "";

        try {
            pageNum = Integer.valueOf(request.getParameter("pageNum"));
            pageSize = Integer.valueOf(request.getParameter("pageSize"));
            roleName = request.getParameter("roleName");
        } catch (Exception e) {
            e.printStackTrace();
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Role> roleList = roleService.findAllByRoleName(roleName);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roleList);


        request.setAttribute("page", rolePageInfo);
        request.setAttribute("searchMap", roleName);
        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/role/role-list.jsp").forward(request, response);
    }

    /**
     * @param request
     * @param response
     * @Description: 分页 角色名称 联合查询 所有 角色(Role)
     * @return:
     * @Author: gjp
     * @Date: 20:35 2020/11/29
     */
    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        /** request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/role/role-list.jsp").forward(request, response); */
        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/role/role-add.jsp").forward(request, response);
    }

    /**
     * @param request
     * @param response
     * @Description: 保存新增的角色
     * @return:
     * @Author: gjp
     * @Date: 20:35 2020/11/29
     */
    public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Role role = BeanUtil.fillBean(request, Role.class);

        roleService.save(role);
        response.sendRedirect(request.getContextPath() + "/system/role?operation=findAll");
    }

    /**
     * @param request
     * @param response
     * @Description: 根据选中的roleId 批量删除角色
     * @return:
     * @Author: gjp
     * @Date: 20:35 2020/11/29
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String deleteArrays = "";
        try {
            deleteArrays = request.getParameter("deleteArrays");
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<String> roleIds = new ObjectMapper().readValue(deleteArrays, new TypeReference<List<String>>() {
        });
        for (String roleId : roleIds) {
            roleService.delete(roleId);
        }
    }

    /**
     * @param request
     * @param response
     * @Description: 角色列表 点击 详情跳转到修改页面
     * @return:
     * @Author: gjp
     * @Date: 20:35 2020/11/29
     */
    public void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roleId = "";
        try {
            roleId = request.getParameter("id");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Role role = roleService.findByRoleId(roleId);
        request.setAttribute("role", role);
        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/role/role-add.jsp").forward(request, response);
    }

    /**
     * @param request
     * @param response
     * @Description: 角色列表修改页面 点击确定后对角色进行修改
     * @return:
     * @Author: gjp
     * @Date: 20:35 2020/11/29
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Role role = new Role();
        try {
            role = BeanUtil.fillBean(request, Role.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        roleService.update(role);
        response.sendRedirect(request.getContextPath() + "/system/role?operation=findAll");
    }

    /**
     * @param request
     * @param response
     * @Description: 角色列表页面 点击更新权限资源 回显该角色所有权限 包括有的和没的
     * @return:
     * @Author: gjp
     * @Date: 20:35 2020/11/29
     */
    public void author(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String roleId = "";
        try {
            roleId = request.getParameter("id");
        } catch (Exception e) {
            e.printStackTrace();
        }

        Role role = roleService.findByRoleId(roleId);
        List<Map> permissionList = roleService.findAllPermissionByRoleId(roleId);

        request.setAttribute("role", role);
        request.setAttribute("permissionList", permissionList);
        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/role/role-permission-add.jsp").forward(request, response);
    }

    /**
     * @param request
     * @param response
     * @Description: 对 修改后角色绑定的权限 进行保存
     * @return:
     * @Author: gjp
     * @Date: 20:35 2020/11/29
     */
    public void savePermissionToRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String roleId = "";
        String[] ids = {};

        try {
            roleId = request.getParameter("roleId");
            ids = request.getParameterValues("ids");
        } catch (Exception e) {
            e.printStackTrace();
        }
        roleService.savePermissionToRole(ids, roleId);
        response.sendRedirect(request.getContextPath() + "/system/role?operation=findAll");
    }
}
